package com.yxq.mdt.app.modules.bottomsheet;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.orhanobut.hawk.Hawk;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yxq.mdt.app.BuildConfig;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.application.UmengShareHelper;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.dialog.ShareRQDialog;
import com.yxq.mdt.app.model.ShareBean;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.personal.model.UserModel;
import com.yxq.mdt.app.net.ApiModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class ShareSheetDialog extends BottomSheetDialogFragment implements UMShareListener {
    Unbinder unbinder;
    @BindView(R.id.wechat_moments_img)
    ImageView wechatMomentsImg;
    @BindView(R.id.wechat_moments_tv)
    TextView wechatMomentsTv;
    @BindView(R.id.wechat_friend_img)
    ImageView wechatFriendImg;
    @BindView(R.id.wechat_friend_tv)
    TextView wechatFriendTv;
    @BindView(R.id.wechat_moments_lint)
    LinearLayout wechatMomentsLint;
    @BindView(R.id.wechat_friend_lint)
    LinearLayout wechatFriendLint;
    ShareBean shareBean;
    //是否显示二维码
    boolean showQRCode = true;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);

    }

    public ShareSheetDialog() {
    }

    public ShareSheetDialog(String msg) {

    }

    public ShareSheetDialog(ShareBean shareBean, boolean showQRCode) {
        this.shareBean = shareBean;
        this.showQRCode = showQRCode;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.share, container, false);
        unbinder = ButterKnife.bind(this, view);
        //分享实体
        if (shareBean == null) {
            shareBean = new ShareBean();
            shareBean.setTitle("一拍即合 等你来捡漏");//标题
            shareBean.setText("社交电商新玩法，好玩让你意想不到。");//文本
            shareBean.setShareType("0");//图文分享
            shareBean.setImageUrl("https://yxqoss.oss-cn-hangzhou.aliyuncs.com/gs_app/init/727c432c9e186a11.png");//LOGO
            if (!"release".equals(BuildConfig.RELEASE)) {
                shareBean.setUrl("http://testpm.quai69.com/register.html?platform=2&parent_id=" + getParentId());//分享URL
            } else {
                shareBean.setUrl("http://pm.quai69.com/register.html?platform=2&parent_id=" + getParentId());
            }
        }
        if (!showQRCode) {
            view.findViewById(R.id.qr_lint).setVisibility(View.GONE);
        }
        return view;
    }

    private String getParentId() {

        return !TextUtils.isEmpty(Constant.getLoginUser().getBinding()) ? Constant.getLoginUser().getBinding() : "0";
    }

    @OnClick({R.id.wechat_moments_lint, R.id.wechat_friend_lint, R.id.qr_lint})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wechat_moments_lint:
                dismiss();
                UmengShareHelper.shareWeChatMoments(getActivity(), this, shareBean);
                break;
            case R.id.wechat_friend_lint:
                dismiss();
                UmengShareHelper.shareWeChat(getActivity(), this, shareBean);
                break;
            case R.id.qr_lint:
                dismiss();
                MyPersonalBean myPersonalBean = Hawk.get(SPKey.SP_KEY_USER_MY_INFO);
                new ShareRQDialog(getActivity(), shareBean.getUrl()).show(myPersonalBean.getGsName());
                break;
        }
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA share_media) {
//        ToastUtils.showShort("分享成功");
        if (shareBean.getShareTypeAPP() == 0) {
            return;
        }
        UserModel.saveGsOperRecord(shareBean.getShareTypeAPP(), shareBean.getHandleId(), 2, new ApiModel.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {

            }

            @Override
            public void onError(String errMsg) {

            }
        });
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
//        ToastUtils.showShort("分享失败");
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
//        ToastUtils.showShort("取消分享");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(getContext()).onActivityResult(requestCode, resultCode, data);
    }


}
